<?php

class Webmix_Model_DbTable_Blocks extends Engine_Db_Table {
	protected $_rowClass = 'Webmix_Model_Block';
    
    public function addMusicFavourite($key){
        if (empty($key)) return false;
        
        $user = Engine_Api::_()->user()->getViewer();
        if (empty($user->user_id )) return false;
        $webmixT = Engine_Api::_()->getDbtable('webmixes','webmix');
        $select = $webmixT->select()
                          ->where('owner_id = ?',$user->user_id)
                          ->where('content_type = ?',Webmix_Api_Core::WEBMIX_CONTENT_TYPE_MUSIC);
        $webmix = $webmixT->fetchRow($select);
        if (!$webmix){
            $webmix = $webmixT->createRow();
            $webmix->title = "Music";
            $webmix->type = Webmix_Model_Webmix::WEBMIX_TYPE_USER;
            $webmix->content_type = Webmix_Api_Core::WEBMIX_CONTENT_TYPE_MUSIC;
            $webmix->owner_id = $viewer->user_id;
            $webmix->country_id = 0;
            $webmix->save();
        }
        
        $musicAlbumT = Engine_Api::_()->getDbtable('Musicalbums','Webmix');
        $select = $musicAlbumT->select()
                              ->where('`key` = ?',$key);
        $music = $musicAlbumT->fetchRow($select);
        if (!$music) {
            return false;
        } 
          
        $blockT = Engine_Api::_()->getDbtable('blocks',"webmix");
        $select = $blockT->select()
                         ->where('webmix_id = ?',$webmix->webmix_id);
        $blocks = $blockT->fetchAll($select);
        if (count($blocks) <= 0){
            $block = $blockT->createRow();
            $block->company_id = $music->musicalbum_id;
            $block->position_x = 0;
            $block->position_y = 0;
            $block->owner_id = $user->user_id;
            $block->webmix_id = $webmix->webmix_id;
            $block->save();
        }  
        
        $positions = array();
        $positions[0] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => false,
                4 => false,
                5 => false,
                6 => false,
                7 => false,
                8 => false
            );
        $positions[1] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => false,
                4 => false,
                5 => false,
                6 => false,
                7 => false,
                8 => false
            );
        $positions[2] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => true,
                4 => true,
                5 => true,                
                6 => false,
                7 => false,
                8 => false
            );
        $positions[3] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => true,
                4 => true,
                5 => true,
                6 => false,
                7 => false,
                8 => false
            );
        $positions[4] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => false,
                4 => false,
                5 => false,
                6 => false,
                7 => false,
                8 => false
            );
        $positions[5] = array(
                0 => false,
                1 => false,
                2 => false,
                3 => false,
                4 => false,
                5 => false,
                6 => false,
                7 => false,
                8 => false
            );
        
        $x = 0;
        $y = 0;
        $break = false;
        foreach($blocks as $block){
            $x = $block->position_x;
            $y = $block->position_y;
            $positions[$x][$y] = true; 
        }
        for($i = 0; $i < 6; $i++){
            for($j = 0; $j < 9; $j++){
                if ($positions[$i][$j] == false){
                    $x = $i;
                    $y = $j;
                    $break = true;
                    break;
                    
                }
            }
            if ($break == true) break;
        }
        
        $block = $blockT->createRow();
        $block->company_id = $music->musicalbum_id;
        $block->position_x = $x;
        $block->position_y = $y;
        $block->owner_id = $user->user_id;
        $block->webmix_id = $webmix->webmix_id;
        $block->save();
                            
        return true;
    }
    public function getBlocks($webmix_id){
        $blockName = $this->info('name');
        
        $webmix = Engine_Api::_()->getItem('webmixes',$webmix_id);
        if (!$webmix) return null;
        
        if ($webmix->content_type == Webmix_Api_Core::WEBMIX_CONTENT_TYPE_MUSIC){
            $albumTable  = Engine_Api::_()->getDbtable('musicalbums','webmix');
            $select = $this->select()
                           ->from(array('block'=>$blockName),'*') 
                           ->setIntegrityCheck(false)
                           ->join(array('musicalbum'=>$albumTable->info('name')),
                                "block.company_id = musicalbum.musicalbum_id",
                                array('title','link','key','photo_id','musicalbum_id','owner_id as company_owner')
                           )
                           ->where('webmix_id = ?',$webmix_id);
            $rows = $this->fetchAll($select);
            return $rows;
        }
        $companyTable = Engine_Api::_()->getDbtable('companies','webmix');
        $companyName = $companyTable->info('name');
        $select = $this->select()
                       ->from(array('block'=>$blockName),'*') 
                       ->setIntegrityCheck(false)
                       ->join(array('company'=>$companyName),
                            "block.company_id = company.company_id",
                            array('title','link','photo_id','company_id','owner_id as company_owner')
                       ) 
                       ->where('webmix_id = ?',$webmix_id);
        $rows = $this->fetchAll($select);
        
        return $rows;
    }
    
 }